﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;

namespace DAL
{
    public class QuanLyThongKeQuery : DataProvide
    {
        public static bool Them(string ma, DateTime ngay)
        {
            SqlConnection ketNoi = null;
            bool ketQua = true;
            try
            {
                ketNoi = KetNoi();
                string insertCommand = "insert into ThongKeBaoCao (MaTKBC, NgayTKBC) values(N'" + ma + "','" + ngay + "')";


                SqlCommand lenh = new SqlCommand(insertCommand, ketNoi);

                lenh.ExecuteNonQuery();
            }
            catch (Exception)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        public static List<ThongKeDAL> Tim(string maTK)
        {
            SqlConnection chuoiKetNoi = null;
            List<ThongKeDAL> dsThongKe = new List<ThongKeDAL>();
            try
            {
                chuoiKetNoi = KetNoi();
                string sqlString = String.Format(@"Select * From ThongKeBaoCao Where MaTKBC like '%"+ maTK +"%'");

                SqlCommand lenh = new SqlCommand(sqlString, chuoiKetNoi);

                SqlDataReader dr = lenh.ExecuteReader();
                while (dr.Read())
                {
                    ThongKeDAL tk = new ThongKeDAL();
                    tk.MaThongKe = dr.GetString(0);
                    if (!dr.IsDBNull(1))
                        tk.NgayThongKe = dr.GetDateTime(1);
                    dsThongKe.Add(tk);
                }

            }
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return dsThongKe;
        }

        public static bool Xoa(string matk)
        {
            SqlConnection ketNoi = null;
            bool ketQua = true;
            try
            {
                ketNoi = KetNoi();
                string insertCommand = "delete from   ThongKeBaoCao  where	MaTKBC ='" + matk + "'";

                SqlCommand lenh = new SqlCommand(insertCommand, ketNoi);

                lenh.ExecuteNonQuery();
            }
            catch (Exception)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        public static bool Sua(string matk, DateTime ngaytk)
        {
            SqlConnection ketNoi = null;
            bool ketQua = true;
            try
            {
                ketNoi = KetNoi();
                string insertCommand = "UPDATE	ThongKeBaoCao SET NgayTKBC = '"+ ngaytk +"' WHERE MaTKBC = '"+ matk +"' ";

                SqlCommand lenh = new SqlCommand(insertCommand, ketNoi);

                lenh.ExecuteNonQuery();

            }
            catch (Exception)
            {
                ketQua = false;

            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
    }
}
